﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BLL;
using Utils;

namespace UI.ModSalary
{
    public partial class ShortageAdd : System.Web.UI.Page
    {
        ShortageBiz shortageBiz = new ShortageBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string shortageID = "0";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["ShortageID"] != null)
            {
                shortageID = Request.Params["ShortageID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                JSUtility.AddConfirm(btnDelete, "您真的要删除该信息吗？");
                txtRewardsTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (shortageID != "0") BindData(shortageID);

            }
        }
        private void BindData(string shortageID)
        {
            DataTable dt = shortageBiz.GetOneInfo(shortageID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
               

                txtRewardsAmount.Text = dt.Rows[0]["RewardsAmount"].ToString();
                txtRewardsReason.Text = dt.Rows[0]["RewardsReason"].ToString();

                string RewardsTime = dt.Rows[0]["RewardsTime"].ToString();
                if (Utils.RegexRule.IsDate(RewardsTime))
                    txtRewardsTime.Text = Convert.ToDateTime(RewardsTime).ToString("yyyy-MM-dd");



                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                    }
                    else
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "ShortageAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "ShortageList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string rewardsTime = txtRewardsTime.Text.Trim();
            if (!RegexRule.IsDate(rewardsTime))
            {
                JSUtility.Alert(this, "日期格式错误");
                return;
            }
            string rewardsAmount = txtRewardsAmount.Text.Trim();
            if (!RegexRule.IsDecimal(rewardsAmount))
            {
                JSUtility.Alert(this, "奖惩金额必须填写数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("RewardsTime", rewardsTime);
            sdList.Add("RewardsTypeID", ddlRewardsTypeName.SelectedValue);
            sdList.Add("RewardsTypeName", ddlRewardsTypeName.SelectedItem.Text);
            sdList.Add("RewardsAmount", rewardsAmount);
            sdList.Add("RewardsReason", txtRewardsReason.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            string info = "";
            if (shortageBiz.Insert("Wage_Shortage", sdList))
                info = "增加信息成功";
            else
                info = "增加信息失败";
            JSUtility.AlertAndRedirect(info, "ShortageAdd.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "ShortageAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "ShortageList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string rewardsTime = txtRewardsTime.Text.Trim();
            if (!RegexRule.IsDate(rewardsTime))
            {
                JSUtility.Alert(this, "日期格式错误");
                return;
            }
            string rewardsAmount = txtRewardsAmount.Text.Trim();
            if (!RegexRule.IsDecimal(rewardsAmount))
            {
                JSUtility.Alert(this, "奖惩金额必须填写数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ShortageID", shortageID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("RewardsTime", rewardsTime);
            sdList.Add("RewardsTypeID", ddlRewardsTypeName.SelectedValue);
            sdList.Add("RewardsTypeName", ddlRewardsTypeName.SelectedItem.Text);
            sdList.Add("RewardsAmount", rewardsAmount);
            sdList.Add("RewardsReason", txtRewardsReason.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (shortageID != "0")
            {
                string info = "";
                if (shortageBiz.Update("Wage_Shortage", sdList))
                {
                    info = "成功更新信息";
                    BindData(shortageID);
                }
                else
                    info = "更新信息失败";
                JSUtility.Alert(this, info);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "ShortageAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "ShortageList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            if (shortageID != "0")
            {
                if (shortageBiz.DeleteOneInfo(shortageID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }



        

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}